<?php

namespace app\modules\api\controllers;

use app\components\API;
use app\components\Api\JMessageApi;
use app\components\Api\Pay;
use app\components\Api\User;
use app\components\Api\UserSystemInfo;
use app\components\Fun;
use app\models\Jobs;
use app\models\user\AvatarModel;
use phpDocumentor\Reflection\Types\Null_;
use \yii\web\Controller;
use Yii;

class AppController extends Controller
{
    /**
     * 全部用户
     */
    const authority_all=1;
    /**
     * 必须登录
     */
    const authority_user=2;
    public $layout = 'main_m';
    public $enableCsrfValidation = false;
    private $apilist = [
        'getNowUserInfo' => [
            'call' => User::class,
            'authority' => self::authority_all
        ],
        'getjiguang'=>[
            'call' => JMessageApi::class,
            'authority' => self::authority_user///user
        ],
        'setUserTypeIntroducer'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getUserTypeIntroducer'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_info_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_info_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_info_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],






        'getusers_baseinfo_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_baseinfo_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_baseinfo_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],






        'setusers_baseinfo_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_baseinfo_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_baseinfo_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],











        'getusers_zhengjianshangchuan_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'getusers_zhengjianshangchuan_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'getusers_zhengjianshangchuan_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],





        'setusers_zhengjianshangchuan_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_zhengjianshangchuan_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_zhengjianshangchuan_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_basechanpin_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_basechanpin_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],





        'setusers_basechanpin_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'setusers_basechanpin_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'getgongchangshebei_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'setgongchangshebei_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],




        'getusers_basechanpinfengge_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_basechanpinfengge_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_basezhiliangbiaozhun_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_basezhiliangbiaozhun_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_gongsihuanjing_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_gongsihuanjing_gs'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],



        'getusers_chanliangchanneng_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_chanliangchanneng_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_zhiliangyaoqiu_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setusers_zhiliangyaoqiu_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getusers_gongchanghuanjing_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],



        'setusers_gongchanghuanjing_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getuser_jiaoyubeijing_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getUserJiaoyubeijingGr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setuser_jiaoyubeijing_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getuser_gongzuojingli_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setuser_gongzuojingli_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],


        'getuser_jiatingchengyuan_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],

        'setuser_jiatingchengyuan_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],




        'getuser_qiuzhiyixiang_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setuser_qiuzhiyixiang_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getqiuzhiyixiang_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setqiuzhiyixiang_gc'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'getuser_ziwopingjia_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'setuser_ziwopingjia_gr'=>[
            'call' => User::class,
            'authority' => self::authority_user///user
        ],
        'upUserPicture'=>[
            'call' => UserSystemInfo::class,
            'authority' => self::authority_all///user
        ],
        'SetDataViewInput'=>[
            'call' => UserSystemInfo::class,
            'authority' => self::authority_user///user
        ],
        'getUserAvatar'=>[
            'call' => UserSystemInfo::class,
            'authority' => self::authority_user///user
        ],

        'getIdentification'=>[
            'call' => UserSystemInfo::class,
            'authority' => self::authority_user///user
        ],







        'createpayhref'=>[
            'call' => Pay::class,
            'authority' => self::authority_user///user
        ],
        'getUserMoney'=>[
            'call' => Pay::class,
            'authority' => self::authority_user///user
        ],
        'getXieyi'=>[
            'call' => UserSystemInfo::class,
            'authority' => self::authority_all///user
        ],


    ];

    public function actionCall($id)
    {
        // exit($id);
        $one = $this->getApi($id);
        if (!$one) {
            return API::echo_json(1, $id . '接口不存在');
        }
        $call = $one['call'];
        $authority = $one['authority'];

        if ($authority == self::authority_user) {

            if (Yii::$app->user->isGuest) {
                return API::echo_json(-1001,"未登录、请登录");
            }
        }
        /*$apilist = explode('@', $call);
        $className = $apilist[0];
        $classCall = isset($apilist[1]) ? $apilist[1] : null;*/
        $className=$call;
        $classCall=$id;
        if (!class_exists($className)) {
            API::echo_json(1, '接口调用失败，接口类不存在' . $className);
        }
        try {
            $new = new $className();
            return $new->$classCall(\Yii::$app->request->post(),[]);
        } catch (Exception $e) {
            return API::echo_json(1, '接口调用失败' . $e->getMessage());
        }
    }

    private function getApi($id)
    {

        return $this->apilist[$id] ?? null;
    }








    /**
     *
     * @apiGroup 系统
     * @api {get} /api/app/avatar  根据用户极光id显示用户头像,
     * @apiParam {string} jid 用户的极光id
     * @apiParam {string} uid 用户的系统id
     * @apiSuccessExample 正确
     * 图片数据
     *
     */
    public function actionAvatar(){
        $jid=$_GET['jid']??'';
        $uid=$_GET['uid']??'';
        if($jid==''&&$uid==''){
            Fun::showmsg("参数错误");
        }
        @header("Content-Type:image/png");
        @header('Cache-Control:max-age=86400');

        echo AvatarModel::getAvatarData($jid,$uid);
        exit;
    }















    public $title = '测试系统';
    public $description = '测试系统';

}
